class _UnionFindSet
{
public:
	_UnionFindSet(size_t size) :_set(size, -1) {}

	int FindRoot(int x)
	{
		if (_set[x] >= 0) _set[x] = FindRoot(_set[x]);

		if (_set[x] < 0)
			return x;
		else
			return _set[x];
	}

	void Union(int x, int y)
	{
		int rootx = FindRoot(x);
		int rooty = FindRoot(y);

		if (rootx != rooty)
		{
			_set[rootx] += _set[rooty];
			_set[rooty] = rootx;
		}
	}

	size_t SetCount()
	{
		size_t num = 0;
		for (size_t i = 0; i < _set.size(); i++)
			if (_set[i] < 0)
				num++;
		return num;
	}

private:
	vector<int> _set;
};
